package com.example.springbott.wx_service;

import com.example.springbott.bean.MarketCollect;
import com.example.springbott.bean.MarketCollectExample;
import com.example.springbott.bean.MarketGoods;
import com.example.springbott.bean.MarketUser;
import com.example.springbott.bean.zeng.CollectListReturnVo;
import com.example.springbott.bean.zeng.CollectListVo;
import com.example.springbott.mapper.MarketCollectMapper;
import com.example.springbott.mapper.MarketGoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: zeng
 * @date: 13:46 2022/11/22
 */

@Service
public class WxCollectServiceImpl implements WxCollectService {
    @Autowired
    MarketCollectMapper marketCollectMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public CollectListVo list(MarketUser user, Byte type, Integer page, Integer limit) {

        Integer id = user.getId();
        //分页操作
        PageHelper.startPage(page, limit);
        //获取该用户的收藏列表
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        criteria.andUserIdEqualTo(id).andDeletedEqualTo(false);
        marketCollectExample.setOrderByClause("update_time"+" "+"desc");
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);

        PageInfo<MarketCollect> marketCollectPageInfo = new PageInfo<>();
        long total = marketCollectPageInfo.getTotal();
        int pages = marketCollectPageInfo.getPages();

        ArrayList<CollectListReturnVo> list = new ArrayList<>();
        for (MarketCollect marketCollect : marketCollects) {
            Integer valueId = marketCollect.getValueId();
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(valueId);
            CollectListReturnVo collectListReturnVo = new CollectListReturnVo();
            collectListReturnVo.setBrief(marketGoods.getBrief());
            collectListReturnVo.setId(marketCollect.getId());
            collectListReturnVo.setPicUrl(marketGoods.getPicUrl());
            collectListReturnVo.setRetailPrice(marketGoods.getRetailPrice());
            collectListReturnVo.setValueId(marketCollect.getValueId());
            collectListReturnVo.setName(marketGoods.getName());
            collectListReturnVo.setType(marketCollect.getType());
            list.add(collectListReturnVo);
        }
        //封装返回对象
        CollectListVo collectListVo = new CollectListVo();
        collectListVo.setList(list);
        collectListVo.setLimit(limit);
        collectListVo.setPage(page);
        collectListVo.setTotal((int) total);
        collectListVo.setPages(pages);

        return collectListVo;
    }

    @Override
    public void addordelete(MarketUser marketUser, Byte type, Integer valueId) {
        // Integer id = 1;
        //
        // //判断收藏or取消收藏
        // MarketCollectExample marketCollectExample = new MarketCollectExample();
        // MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        // criteria.andValueIdEqualTo(valueId).andTypeEqualTo(type).andDeletedEqualTo(false);
        // long count = marketCollectMapper.countByExample(marketCollectExample);
        //
        // //如果在收藏表里没有，则收藏
        // if (count == 0) {
        //     MarketCollect marketCollect = new MarketCollect();
        //     Date date = new Date();
        //     marketCollect.setUserId(id);
        //     marketCollect.setValueId(valueId);
        //     marketCollect.setType(type);
        //     marketCollect.setAddTime(date);
        //     marketCollect.setUpdateTime(date);
        //     marketCollect.setDeleted(false);
        //     marketCollectMapper.insert(marketCollect);
        // } else {
        //     //收藏表里有，改为取消收藏
        //     Date updateTime = new Date();
        //     marketCollectMapper.updateOfCollect(valueId,updateTime, true);
        // }
        Integer id = marketUser.getId();

        // //判断收藏or取消收藏
        MarketCollect marketCollect = marketCollectMapper.select(type, valueId);
        //新增收藏
        if (marketCollect == null) {
            MarketCollect collect = new MarketCollect();
            collect.setId(null);
            collect.setUserId(id);
            collect.setValueId(valueId);
            collect.setType(type);
            collect.setAddTime(new Date());
            collect.setUpdateTime(new Date());
            collect.setDeleted(false);

            marketCollectMapper.insert(collect);
        } else {
            //收藏表有并且deleted为true，deleted变为false
            if (marketCollect.getDeleted()== true) {
                marketCollect.setDeleted(false);
                marketCollect.setUpdateTime(new Date());
                marketCollectMapper.updateInCollect(marketCollect);
            }
            else if (marketCollect.getDeleted()==false) {
                marketCollect.setDeleted(true);
                marketCollect.setUpdateTime(new Date());
                marketCollectMapper.updateInCollect(marketCollect);
            }
        }

    }
}
